//
//  ZPTabBar.swift
//  ZPWeiBo01
//
//  Created by Nevermore on 16/9/1.
//  Copyright © 2016年 Nevermore. All rights reserved.
//

import UIKit

protocol ZPTabBarDelegate:NSObjectProtocol{
    
    func tabBarButtonClick()
}



class ZPTabBar: UITabBar {

    weak var zpdelegate:ZPTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK - 撰写按钮点击
    func buttonClick(){
        
        zpdelegate?.tabBarButtonClick()
        
    }
    
    
    //MARK: - 设置视图
    private func setupUI(){
        
        addSubview(composeButton)
        
    }
    
    //子空间设置Fram
    override func layoutSubviews() {
        super.layoutSubviews()
        let childw = self.frame.width * 0.2
        var index: CGFloat = 0
        for value in self.subviews {
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                value.frame.size.width = childw;
                value.frame.origin.x = index * childw
                index++
                if index == 2 {
                    index++
                }
            }
        }
        
        
        
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
    }
    
    
    
    //MARK: - 懒加载控件
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchDragInside)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.sizeToFit()
        
        return button
    }()
}
